﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using VehicleManagement.ControllersServices;
using System.Collections;

namespace VehicleManagement.Controllers
{
    [CustomerAuthorize(Roles = "负责人")]
    public class ApprovalController : Controller
    {
        ApprovalServices approvalServices = new ApprovalServices();

        //
        // GET: /Approval/

        /// <summary>
        /// 显示所有审批表，负责人权限
        /// </summary>
        /// <returns></returns>
        //public ActionResult Index()
        //{
        //    return View(approvalServices.GetAllApprovals());
        //}

        //
        // GET: /Approval/Details/2

        public ActionResult Details(int id)
        {
            if (Session["CompanyName"] == null)
                return View("/Home/Login");
            Hashtable ht = new Hashtable();
            ht.Add("companyName", Session["CompanyName"].ToString());
            ht.Add("approvalID", id);

            Approval approval = approvalServices.GetApprovalByApprovalID(ht);
            if (approval == null || approval.ApplyID == 0)
            {
                return View("/Home/Login");
            }
            return View(approval);
        }

        //
        // get: /Approval/IsApprovalExist/5(applyID)

        public void IsApprovalExist(int id)
        {
                int hasApproval = 0;
                hasApproval = approvalServices.IsApprovalExist(id);

                if (hasApproval > 0)
                {
                    Response.Redirect("../Details/" + hasApproval);
                }
                else
                {
                    Response.Redirect("../Home/Login");
                }
        }

        //
        // GET: /Approval/Create

        //public ActionResult Create()
        //{
        //    return View();
        //}

        ////
        //// POST: /Approval/Create
        //[HttpPost]
        //public ActionResult Create(Approval approval)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        object obj = approvalServices.CreateApproval(approval);
        //        return RedirectToAction("Index");
        //    }
        //    return View(approval);
        //}

        //
        // GET: /Approval/Edit/5

        public ActionResult Edit(int id)
        {
            if (Session["CompanyName"] == null)
                return View("/Home/Login");
            Hashtable ht = new Hashtable();
            ht.Add("companyName", Session["CompanyName"].ToString());
            ht.Add("approvalID", id);
            Approval approval = approvalServices.GetApprovalByApprovalID(ht);
            if (approval == null || approval.ApplyID == 0)
            {
                return View("/Home/Login");
            }
            return View(approval);
        }

        //
        // POST: /Approval/Edit
        [HttpPost]
        public ActionResult Edit(Approval approval)
        {
            if (ModelState.IsValid)
            {
                int rows = approvalServices.EditApproval(approval);
                return RedirectToAction("../Apply/Index");
            }
            return View(approval);
        }

        //
        // GET: /Approval/DeleteApproval/5

        //public ActionResult DeleteApproval(int id)
        //{
        //    return View(approvalServices.GetApprovalByApprovalID(id));
        //}

        //
        // POST: /Approval/Delete/5

        [HttpPost, ActionName("DeleteApproval")]
        public ActionResult DeleteConfirmed(int id)
        {


            if (Request.IsAjaxRequest())
            {
                if (GetApprovalState(id))
                    return Json(new { Result = "false", Message = "这条审批已经通过，不能删除" });
                else
                {
                    int i = approvalServices.DeleteApproval(id);
                    if (i > 0)
                    {
                        return Json(new { Result = "true", Message = "删除成功" });
                    }
                    else
                    {
                        return Json(new { Result = "false", Message = "删除失败" });
                    }
                }
            }
            else
            {
                return Json(new { Result = "false", Message = "该方法只能通过AJAX方式调用" });
            }
        }

        /// <summary>
        /// 获取审批表状态 通过或不通过
        /// </summary>
        /// <param name="approvalID">审批表ID</param>
        /// <returns>审批表状态 true false</returns>
        [NonAction]
        private bool GetApprovalState(int approvalID)
        {
            ApprovalServices approvalServices = new ApprovalServices();
            bool approvalState = false;
            approvalState = approvalServices.GetApprovalState(approvalID);
            return approvalState;
        }

        ////////////////////新增方法//////////////////////////
        //
        // GET: /Approval/CheckPathMap

        public ActionResult CheckPathMap()
        {
            IList<Approval> lstPathMap = SqlMapperManager.Instance.Get().QueryForList<Approval>("SELECT_ALL_PATHMAP", null);
            return View(lstPathMap);
        }
        //
        // GET: /Approval/PathMapDetail/1

        public ActionResult PathMapDetail(int id)
        {
            Approval approval = SqlMapperManager.Instance.Get().QueryForObject<Approval>("SELECT_PATHMAP_BY_ID", id);
            if (approval != null)
            {
                return View(approval);
            }
            else
            {
                return Json(new { Result = "false", Message = "路线不存在" }, JsonRequestBehavior.AllowGet);
            }
        }
        //
        // GET: /Approval/GetPathMap/1

        public void GetPathMap(int id)
        {
            Approval approval = SqlMapperManager.Instance.Get().QueryForObject<Approval>("SELECT_PATHMAP_BY_ID", id);
            Response.ContentType = "image/jpg";
            Response.BinaryWrite(approval.PathMap);
        }

        //
        // POST: /Approval/ConfirmPathMap/5

        [HttpPost, ActionName("ConfirmPathMap")]
        public ActionResult ConfirmPathMap(int id)
        {
            if (Request.IsAjaxRequest())
            {
                if (GetConfirmState(id))
                    return Json(new { Result = "false", Message = "这条路线已经通过审核，不能重复确认" });
                else
                {
                    int i = SqlMapperManager.Instance.Get().Update("CONFIRM_PATH", id);
                    if (i > 0)
                    {
                        return Json(new { Result = "true", Message = "路线审核成功" });
                    }
                    else
                    {
                        return Json(new { Result = "false", Message = "路线审核失败" });
                    }
                }
            }
            else
            {
                return Json(new { Result = "false", Message = "该方法只能通过AJAX方式调用" });
            }
        }

        public bool GetConfirmState(int id)
        {
            Approval approval = SqlMapperManager.Instance.Get().QueryForObject<Approval>("SELECT_PATHMAP_BY_ID", id);
            return approval.PathConfirm;
        }
        /////////////////////////////////////////////////////
    }
}
